//
//  XJRegistSetInfo.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/8.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJRegistSetInfo.h"

@interface XJRegistSetInfo ()<UITextFieldDelegate>

//头像
//@property (nonatomic ,strong) UIImageView * iconImgV;
@property (nonatomic, strong) UILabel * tipsLab;
//性别
@property (nonatomic, strong) ZYTextField * setSexTextField;
@property (nonatomic ,strong) UIButton * menBtn;
@property (nonatomic, strong) UIButton * womenBtn;
//进入
@property (nonatomic, strong) UIButton * intoBtn;

@end

@implementation XJRegistSetInfo

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = WHITE_COLOR;
        [self setupSubviews];
    }
    return self;
    
}

- (UIImageView *)iconImgV{
    
    if (!_iconImgV) {
        UIImageView * iconImgV = [UIImageView new];
        self.iconImgV = iconImgV;
        [self addSubview:iconImgV];
    }
    return _iconImgV;
}

- (UILabel *)tipsLab{
    
    if (!_tipsLab) {
        UILabel * tipsLab = [UILabel new];
        self.tipsLab = tipsLab;
        [self.iconImgV addSubview:tipsLab];
    }
    return _tipsLab;
}

- (ZYTextField *)setInfoNickTextField{
    
    if (!_setInfoNickTextField) {
        ZYTextField * setInfoNickTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(22), CGRectGetMaxY(self.iconImgV.frame)+ADAPTHeight(20), KWidth - ADAPTWidth(22)*2, ADAPTHeight(50)) placeholder:@"请设置昵称12个字符以内" keyboardType:UIKeyboardTypeDefault leftViewImageName:@"login_name" textEntry:NO];
        self.setInfoNickTextField = setInfoNickTextField;
        [self addSubview:setInfoNickTextField];
    }
    return _setInfoNickTextField;
}

- (ZYTextField *)setSexTextField{
    
    if (!_setSexTextField) {
        ZYTextField * setSexTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(22), CGRectGetMaxY(self.setInfoNickTextField.frame)+0.5,KWidth - ADAPTWidth(22)*2, ADAPTHeight(50)) placeholder:nil keyboardType:UIKeyboardTypeDefault leftViewImageName:@"login_gender" textEntry:NO];
        self.setSexTextField = setSexTextField;
        [self addSubview:setSexTextField];
    }
    return _setSexTextField;
}

- (UIButton *)menBtn{
    if (!_menBtn) {
        UIButton * menBtn = [UIButton new];
        self.menBtn = menBtn;
        [self.setSexTextField addSubview:menBtn];
    }
    return _menBtn;
}

- (UIButton *)womenBtn{
    if (!_womenBtn) {
        UIButton * womenBtn = [UIButton new];
        self.womenBtn = womenBtn;
        [self.setSexTextField addSubview:womenBtn];
    }
    return _womenBtn;
}

- (ZYTextField *)setInfoPasswdTextField{
    
    if (!_setInfoPasswdTextField) {
        ZYTextField * setInfoPasswdTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(22), CGRectGetMaxY(self.setSexTextField.frame)+0.5,KWidth - ADAPTWidth(22)*2, ADAPTHeight(50)) placeholder:@"请输入新密码6-20位" keyboardType:UIKeyboardTypeDefault leftViewImageName:@"login_password" textEntry:YES];
        self.setInfoPasswdTextField = setInfoPasswdTextField;
        [self addSubview:setInfoPasswdTextField];
    }
    return _setInfoPasswdTextField;
}

- (ZYTextField *)setInfoRePasswdTextField{
    if (!_setInfoRePasswdTextField) {
        ZYTextField * setInfoRePasswdTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(22), CGRectGetMaxY(self.setInfoPasswdTextField.frame)+0.5,KWidth - ADAPTWidth(22)*2, ADAPTHeight(50)) placeholder:@"请确认新密码6-20位" keyboardType:UIKeyboardTypeDefault leftViewImageName:@"login_password" textEntry:YES];
        self.setInfoRePasswdTextField = setInfoRePasswdTextField;
        [self addSubview:setInfoRePasswdTextField];
    }
    return _setInfoRePasswdTextField;
}

- (UIButton *)intoBtn{
    
    if (!_intoBtn) {
        UIButton * intoBtn = [UIButton new];
        self.intoBtn = intoBtn;
        [self addSubview:intoBtn];
    }
    return _intoBtn;
}

- (void)setupSubviews{
    
    self.iconImgV.frame =  CGRectMake(KWidth/2-60, ADAPTHeight(30), 120, 120);
    self.iconImgV.backgroundColor = XJCOLOR_RGB(246, 247, 249);
    self.iconImgV.layer.masksToBounds = YES;
    self.iconImgV.layer.cornerRadius = self.iconImgV.frame.size.width/2;
    self.iconImgV.layer.borderWidth = 0.5;
    self.iconImgV.layer.borderColor = XJCOLOR_RGB(206, 211, 213).CGColor;
    
    UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(setIconImgTap)];
    self.iconImgV.userInteractionEnabled = YES;
    [self.iconImgV addGestureRecognizer:tapRecognizer];
    
    self.tipsLab.text = @"点击上传头像";
    self.tipsLab.font = XJFONTSET_R(14);
    self.tipsLab.textColor = XJCOLOR_RGB(185, 194, 197);
    
    CGFloat tipsLabW = [UILabel labelWithLabel:self.tipsLab getWidthWithTitle:self.tipsLab.text font:self.tipsLab.font];
    CGFloat tipsLabH = [UILabel labelWithLabel:self.tipsLab getHeightByWidth:tipsLabW title:self.tipsLab.text font:self.tipsLab.font];
    
    self.tipsLab.sd_layout
    .centerXEqualToView(self.iconImgV)
    .centerYEqualToView(self.iconImgV)
    .widthIs(tipsLabW)
    .heightIs(tipsLabH);
    
    self.setInfoNickTextField.delegate = self;

    self.setSexTextField.delegate = self;
    
    self.menBtn.frame = CGRectMake(ADAPTWidth(47), ADAPTHeight(10), ADAPTWidth(79), ADAPTHeight(50)-ADAPTHeight(10)*2);
    self.menBtn.backgroundColor = WHITE_COLOR;
    [self.menBtn setTitle:@"男" forState:UIControlStateNormal];
    [self.menBtn setTitleColor:XJCOLOR_RGB(17, 17, 17) forState:UIControlStateNormal];
    self.menBtn.titleLabel.font = XJFONTSET_R(15);
    self.menBtn.layer.masksToBounds = YES;
    self.menBtn.layer.cornerRadius = self.menBtn.frame.size.height/2;
    self.menBtn.layer.borderColor = XJCOLOR_RGB(157, 165, 168).CGColor;
    self.menBtn.layer.borderWidth = 0.5;
    [self.menBtn addTarget:self action:@selector(menBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.womenBtn.frame = CGRectMake(CGRectGetMaxX(self.menBtn.frame)+ADAPTWidth(15), ADAPTHeight(10), ADAPTWidth(79), ADAPTHeight(50)-ADAPTHeight(10)*2);
    self.womenBtn.backgroundColor = WHITE_COLOR;
    [self.womenBtn setTitle:@"女" forState:UIControlStateNormal];
    [self.womenBtn setTitleColor:XJCOLOR_RGB(17, 17, 17) forState:UIControlStateNormal];
    self.womenBtn.titleLabel.font = XJFONTSET_R(15);
    self.womenBtn.layer.masksToBounds = YES;
    self.womenBtn.layer.cornerRadius = self.womenBtn.frame.size.height/2;
    self.womenBtn.layer.borderColor = XJCOLOR_RGB(157, 165, 168).CGColor;
    self.womenBtn.layer.borderWidth = 0.5;
    [self.womenBtn addTarget:self action:@selector(womenBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.setInfoPasswdTextField.delegate = self;
    self.setInfoRePasswdTextField.delegate = self;
    
    self.intoBtn.backgroundColor = MAIN_COLOR;
    [self.intoBtn setTitle:@"进入新疆商城" forState:UIControlStateNormal];
    [self.intoBtn setTitleColor:XJCOLOR_RGB(17, 17, 17) forState:UIControlStateNormal];
    [self.intoBtn setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];
    self.intoBtn.titleLabel.font = XJFONTSET_R(15);
    self.intoBtn.layer.masksToBounds = YES;
    self.intoBtn.layer.cornerRadius = ADAPTHeight(40)/2;
    [self.intoBtn addTarget:self action:@selector(intoBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.intoBtn.sd_layout
    .leftSpaceToView(self, ADAPTWidth(25))
    .rightSpaceToView(self, ADAPTWidth(25))
    .topSpaceToView(self.setInfoRePasswdTextField, ADAPTHeight(30))
    .heightIs(ADAPTHeight(40));
    
}


#pragma mark UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (self.setSexTextField == textField) {
        return NO;
    }else{
        return YES;
    }
}

#pragma mark 点击事件

- (void)setIconImgTap{
    
//    XJLog(@"上传头像");
    
    if ([self.delegate respondsToSelector:@selector(clickUpdateImage)]) {
        [self.delegate clickUpdateImage];
    }
}

- (void)menBtnClick:(UIButton *)btn{
    XJLog(@"   %@  ",btn.titleLabel.text)

//    self.sexStr = btn.titleLabel.text;

    self.sexStr = @"1"; //1:男
    self.womenBtn.selected=NO;
    self.menBtn.selected=YES;
    self.menBtn = btn;
    
    [self.menBtn setBackgroundColor:MAIN_COLOR];
    self.menBtn.layer.masksToBounds = YES;
    self.menBtn.layer.borderWidth = 0;
    
    [self.womenBtn setBackgroundColor:WHITE_COLOR];
    self.womenBtn.layer.masksToBounds = YES;
    self.womenBtn.layer.borderWidth = 0.5;
}

- (void)womenBtnClick:(UIButton *)btn{
    XJLog(@"   %@  ",btn.titleLabel.text)

//    self.sexStr = btn.titleLabel.text;
    self.sexStr = @"2"; //2:女
    
    self.womenBtn.selected=YES;
    self.menBtn.selected=NO;
    self.womenBtn = btn;
    
    [self.womenBtn setBackgroundColor:MAIN_COLOR];
    self.womenBtn.layer.masksToBounds = YES;
    self.womenBtn.layer.borderWidth = 0;
    
    [self.menBtn setBackgroundColor:WHITE_COLOR];
    self.menBtn.layer.masksToBounds = YES;
    self.menBtn.layer.borderWidth = 0.5;
}


- (void)intoBtnClick{
//    XJLog(@"进入新疆商城");
    if ([self.delegate respondsToSelector:@selector(clickRegistBtn)]) {
        [self.delegate clickRegistBtn];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.setInfoNickTextField endEditing:YES];

    [self.setInfoPasswdTextField endEditing:YES];
    
    [self.setInfoRePasswdTextField endEditing:YES];
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
